#pragma once
#include <iostream>
#include <pthread.h>

namespace wgm
{
    class Mutex
    {
    public:
        Mutex()
        {
            pthread_mutex_init(&_lock, nullptr);
        }

        void Lock()
        {
            pthread_mutex_lock(&_lock);
        }

        void Unlock()
        {
            pthread_mutex_unlock(&_lock);
        }

        ~Mutex()
        {
            pthread_mutex_destroy(&_lock);
        }

    private:
        pthread_mutex_t _lock;
    };

    class MutexGuard
    {
    public:
        MutexGuard(Mutex &mutex) : _mutex(mutex)
        {
            std::cout << "加锁" << std::endl;
            _mutex.Lock();
        }

        ~MutexGuard()
        {
            std::cout << "解锁" << std::endl;
            _mutex.Unlock();
        }

    private:
        Mutex &_mutex;
    };
}